//
//  Filter404.swift
//  ECApp
//
//  Created by 程平 on 2017/10/21.
//

import Foundation
import PerfectHTTP
import PerfectLogger
struct Filter404:HTTPResponseFilter {
    func filterHeaders(response: HTTPResponse, callback: (HTTPResponseFilterResult) -> ()) {
        LogFile.info("404过滤")
        if case .notFound = response.status {
            response.bodyBytes.removeAll()
            let result:[String : Any] = ["code":100,"message":"访问地址有误","data":""]
            try! response.setBody(json: result)
            response.setHeader(HTTPResponseHeader.Name.contentLength, value: "\(response.bodyBytes.count)")
            response.setHeader(HTTPResponseHeader.Name.contentType, value: "application/json;charset=utf-8")
            response.status = HTTPResponseStatus.ok
            callback(.done)
        } else {
            callback(.continue)
        }
        
    }
    
    func filterBody(response: HTTPResponse, callback: (HTTPResponseFilterResult) -> ()) {
        callback(.continue)
        
    }
    
  
    
}
